package com.ecarx.vhaltests.hy11.testcases.climate;

import android.car.VehicleAreaSeat;
import android.car.VehiclePropertyIds;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VhalUnitTestBase;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;


@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HvacDualOnTest extends VhalUnitTestBase {

    private static final String TAG = "HvacDualOnTest";
    private static boolean halValue;
    private static int halStatus;

    private static int halArea;

    private static final int HVAC_DUAL_ON = VehiclePropertyIds.HVAC_DUAL_ON;

    private static final int HVAC_DUAL_ON_AREA = VehicleAreaSeat.SEAT_ROW_1_LEFT | VehicleAreaSeat.SEAT_ROW_1_RIGHT | VehicleAreaSeat.SEAT_ROW_2_LEFT
            | VehicleAreaSeat.SEAT_ROW_2_CENTER | VehicleAreaSeat.SEAT_ROW_2_RIGHT;

    private static final int SERVICE_ID_CLIMATE = 112;
    private static final int OP_ID_CLIMATE = 0xc8;


    final static Object lock = new Object();

    @BeforeClass
    public static void testInit() {
        mCarPropertyMgr.registerCallback(carPropertyEventCallback, HVAC_DUAL_ON, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        setConfig(OP_ID_CLIMATE);
        delay();
    }

    @Test
    public void testHvacDualOnAvailable_02() {
        // 设置 空调开关 不可用
        setHvacDualONAvail(3, 0);
        testHvacDualOnAvailable(HVAC_DUAL_ON_AREA, false);
        delay();
    }

    @Test
    public void testHvacDualOnAvailable_01() {
        // 设置 空调开关 可用
        setHvacDualONAvail(1, 0);
        testHvacDualOnAvailable(HVAC_DUAL_ON_AREA, true);
        delay();
    }


    @Test
    public void testHvacDualOnGet_01() {
        setHvacDualONAvail(1, 0);
        // 测试打开空调后，获取空调状态
        testHvacDualOnGet(HVAC_DUAL_ON_AREA, true);
        delay();
    }

    @Test
    public void testHvacDualOnGet_02() {
        setHvacDualONAvail(1, 1);
        // 测试关闭空调后，获取空调状态
        testHvacDualOnGet(HVAC_DUAL_ON_AREA, false);
        delay();
    }

    @Test
    public void testHvacDualOnSet_01() {
        // 设置 空调开关 可用
        setHvacDualONAvail(1, 0);
        // 测试打开空调
        testHvacDualOnSet(HVAC_DUAL_ON_AREA, true);
        delay();
    }

    @Test
    public void testHvacDualOnSet_02() {
        setHvacDualONAvail(1, 1);
        // 测试关闭空调
        testHvacDualOnSet(HVAC_DUAL_ON_AREA, false);
        delay();
    }

    private static CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback = new CarPropertyManager.CarPropertyEventCallback() {
        @Override
        public void onChangeEvent(CarPropertyValue carPropertyValue) {
            try {
                halArea = carPropertyValue.getAreaId();
                Log.d(TAG, "HVAC_DUAL_ON onChangeEvent value: propertyId ：" + carPropertyValue.getPropertyId() + " propertyValue：" + carPropertyValue.getValue() + "");
                Log.d(TAG, "HVAC_DUAL_ON onChangeEvent Status：" + carPropertyValue.getStatus());
                Log.d(TAG, "HVAC_DUAL_ON onChangeEvent area：" + carPropertyValue.getAreaId());
                halValue = (boolean) carPropertyValue.getValue();
                halStatus = carPropertyValue.getStatus();
                synchronized (lock) {
                    lock.notify();
                }
                Log.d(TAG, "HVAC_DUAL_ON onChangeEvent halValue :" + halValue);

            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }

        @Override
        public void onErrorEvent(int i, int i1) {

        }
    };


    public void lockWait() {
        try {
            synchronized (lock) {
                lock.wait(500);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public void testHvacDualOnAvailable(int area, boolean avail) {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(HVAC_DUAL_ON, area);
        Log.d(TAG, "testHvacDualOnAvailable isAvail：" + isAvail);
        Assert.assertEquals(avail, isAvail);
    }

    public void testHvacDualOnSet(int area, boolean value) {
        Log.d(TAG, "testHvacDualOnSet area：" + area + " value: " + value);
        mCarPropertyMgr.setBooleanProperty(HVAC_DUAL_ON, area, value);
        lockWait();
        Assert.assertEquals(value, halValue);
    }

    public void testHvacDualOnGet(int area, boolean value) {
        Log.d(TAG, "testHvacDualOnGet area：" + area + " value: " + value);
        mCarPropertyMgr.setBooleanProperty(HVAC_DUAL_ON, area, value);
        lockWait();
        boolean isHvacDualOn = mCarPropertyMgr.getBooleanProperty(HVAC_DUAL_ON, area);
        Assert.assertEquals(value, isHvacDualOn);
    }

    public void setHvacDualONAvail(int avail, int onOff) {
        Log.d(TAG, "setHvacDualONAvail avail：" + avail);
        IpcpClient ipcpClient = new IpcpClient();
        String[] config_indexs = {"paclsync.availability", "paclsync.data"};
        int[] datas = {avail, onOff}; //
        boolean bb = ipcpClient.setClimaSignal(SERVICE_ID_CLIMATE, OP_ID_CLIMATE, config_indexs, datas);
        Assert.assertTrue(bb);
        lockWait();
    }


    private static void setConfig(int OperId) {
        IpcpClient ipcpClient = new IpcpClient();
        int climateOperId[] = {OperId};
        ipcpClient.setConfig(SERVICE_ID_CLIMATE, climateOperId);
    }

}
